package com.cws4cloud.cws.sdAdmin.biz.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cws4cloud.cws.common.core.util.R;
import com.cws4cloud.cws.common.log.annotation.SysLog;
import com.cws4cloud.cws.sdAdmin.api.entity.SysLocation;
import com.cws4cloud.cws.sdAdmin.api.service.SysLocationService;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;


/**
 * 
 *
 * @author zhulj
 * @date 2021-03-09 13:37:22
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/syslocation" )
@Api(value = "syslocation", tags = "地址1管理")
public class SysLocationController {

    private final  SysLocationService sysLocationService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param sysLocation 
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('syslocation_get')" )
    public R getSysLocationPage(Page page, SysLocation sysLocation) {
        return R.ok(sysLocationService.page(page, Wrappers.query(sysLocation)));
    }

	/**
	 * 根据父id查询
	 * @param parentId 父级id
	 * @return
	 */
	@ApiOperation(value = "根据父id查询", notes = "根据父id查询")
	@GetMapping("/listByParent" )
	public R listByParent(Integer parentId) {
		return R.ok(sysLocationService.list(Wrappers.<SysLocation>lambdaQuery().eq(parentId!=null,SysLocation::getParentId,parentId).isNull(parentId==null,SysLocation::getParentId)));
	}


    /**
     * 通过id查询
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('syslocation_get')" )
    public R getById(@PathVariable("id" ) Integer id) {
        return R.ok(sysLocationService.getById(id));
    }

    /**
     * 新增
     * @param sysLocation 
     * @return R
     */
    @ApiOperation(value = "新增", notes = "新增")
    @SysLog("新增" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('syslocation_add')" )
    public R save(@RequestBody SysLocation sysLocation) {
        return R.ok(sysLocationService.save(sysLocation));
    }

    /**
     * 修改
     * @param sysLocation 
     * @return R
     */
    @ApiOperation(value = "修改", notes = "修改")
    @SysLog("修改" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('syslocation_edit')" )
    public R updateById(@RequestBody SysLocation sysLocation) {
        return R.ok(sysLocationService.updateById(sysLocation));
    }

    /**
     * 通过id删除
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除", notes = "通过id删除")
    @SysLog("通过id删除" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('syslocation_del')" )
    public R removeById(@PathVariable Integer id) {
        return R.ok(sysLocationService.removeById(id));
    }

}
